<!DOCTYPE html><html lang="en"><head><title>WebhookCreate.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="WebhookCreate.aspx"><meta name="groc-project-path" content="WebhookCreate.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">WebhookCreate.aspx.cs</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper">﻿<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Web;
<span class="hljs-keyword">using</span> System.Web.UI;
<span class="hljs-keyword">using</span> System.Web.UI.WebControls;
<span class="hljs-keyword">using</span> PayPal.Api;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> WebhookCreate : BaseSamplePage
    {
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> Helper method for creating a new, unique webhook URL to be used by the webhook sample pages.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;returns&gt;</span>A new, unique webhook URL.<span class="hljs-xmlDocTag">&lt;/returns&gt;</span></span>
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> <span class="hljs-title">GetNewWebhookUrl</span>()
        {
            <span class="hljs-keyword">return</span> <span class="hljs-string">"https://www.paypal.com/paypal_webhook_samples/"</span> + Guid.NewGuid().ToString();
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> Helper method for creating a new webhook object to be used by the webhook sample pages.</span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;returns&gt;</span>A new Webhook object.<span class="hljs-xmlDocTag">&lt;/returns&gt;</span></span>
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Webhook <span class="hljs-title">GetNewWebhook</span>()
        {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Webhook
            {
                url = GetNewWebhookUrl(),
                event_types = <span class="hljs-keyword">new</span> List&lt;WebhookEventType&gt;
                {
                    <span class="hljs-keyword">new</span> WebhookEventType
                    {
                        name = <span class="hljs-string">"PAYMENT.AUTHORIZATION.CREATED"</span>
                    },
                    <span class="hljs-keyword">new</span> WebhookEventType
                    {
                        name = <span class="hljs-string">"PAYMENT.AUTHORIZATION.VOIDED"</span>
                    }
                }
            };
        }

        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();

            <span class="hljs-keyword">var</span> webhook = GetNewWebhook();

            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create webhook"</span>, webhook);
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            <span class="hljs-keyword">var</span> createdWebhook = webhook.Create(apiContext);

            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdWebhook);
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Retrieve a webhook"</span>, description: <span class="hljs-string">"ID: "</span> + createdWebhook.id);
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            <span class="hljs-keyword">var</span> retrievedWebhook = Webhook.Get(apiContext, createdWebhook.id);

            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(retrievedWebhook);
            <span class="hljs-comment">//--------------------</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Cleanup</p></div></div><div class="code"><div class="wrapper">            retrievedWebhook.Delete(apiContext);
        }
    }
}</div></div></div></div></body></html>